package example

import (
	"context"
	"fmt"
	"time"
)

func T1(){
	ctx , cancel := context.WithCancel(context.Background())
	for n:= range gen(ctx){
		fmt.Println(n)
		if n == 5{
			cancel()
			break
		}
	}
}

func gen(ctx context.Context) <-chan int{
	ch := make(chan int)
	go func(){
		var n int
		for  {
			select {
			case <-ctx.Done():
				return
			case ch <- n:
				n++
				time.Sleep(time.Second)
			}
		}
	}()
	return ch
}
